using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WeChatFileManagementWinApp
{
    /// <summary>
    /// 设置窗体，用于管理搜索目录设置
    /// </summary>
    public partial class SettingsForm : Form
    {
        private readonly SettingsManager _settingsManager;
        private readonly List<string> _directories;

        /// <summary>
        /// 初始化设置窗体
        /// </summary>
        /// <param name="settingsManager">设置管理器实例</param>
        public SettingsForm(SettingsManager settingsManager)
        {
            InitializeComponent();
            _settingsManager = settingsManager;
            _directories = new List<string>(_settingsManager.LoadSearchDirectories());
            LoadDirectories();
        }

        /// <summary>
        /// 加载目录列表到界面
        /// </summary>
        private void LoadDirectories()
        {
            listBoxDirectories.Items.Clear();
            foreach (var dir in _directories)
            {
                listBoxDirectories.Items.Add(dir);
            }
        }

        /// <summary>
        /// 添加目录按钮点击事件
        /// </summary>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (var dialog = new FolderBrowserDialog())
            {
                dialog.Description = "选择要搜索的微信文件目录";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string path = dialog.SelectedPath;
                    if (!_directories.Contains(path))
                    {
                        _directories.Add(path);
                        listBoxDirectories.Items.Add(path);
                    }
                    else
                    {
                        MessageBox.Show("该目录已经添加", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        /// <summary>
        /// 删除目录按钮点击事件
        /// </summary>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            int selectedIndex = listBoxDirectories.SelectedIndex;
            if (selectedIndex >= 0)
            {
                _directories.RemoveAt(selectedIndex);
                listBoxDirectories.Items.RemoveAt(selectedIndex);
            }
        }

        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                _settingsManager.SaveSearchDirectories(_directories);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存设置失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// 上移按钮点击事件
        /// </summary>
        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            int selectedIndex = listBoxDirectories.SelectedIndex;
            if (selectedIndex > 0)
            {
                string item = _directories[selectedIndex];
                _directories.RemoveAt(selectedIndex);
                _directories.Insert(selectedIndex - 1, item);
                LoadDirectories();
                listBoxDirectories.SelectedIndex = selectedIndex - 1;
            }
        }

        /// <summary>
        /// 下移按钮点击事件
        /// </summary>
        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            int selectedIndex = listBoxDirectories.SelectedIndex;
            if (selectedIndex >= 0 && selectedIndex < _directories.Count - 1)
            {
                string item = _directories[selectedIndex];
                _directories.RemoveAt(selectedIndex);
                _directories.Insert(selectedIndex + 1, item);
                LoadDirectories();
                listBoxDirectories.SelectedIndex = selectedIndex + 1;
            }
        }

        // The following members are generated by the UI designer in SettingsForm.Designer.cs
        // private IContainer components = null;
        // private ListBox listBoxDirectories;
        // private TextBox textBoxDirectory;
        // private Button buttonBrowse;
        // private Button buttonAdd;
        // private Button buttonRemove;
        // private Button buttonSave;\n        // private Button buttonCancel;

        // protected override void Dispose(bool disposing)
        // {
        //     if (disposing && (components != null))
        //     {
        //         components.Dispose();
        //     }
        //     base.Dispose(disposing);
        // }

        // private void InitializeComponent()
        // {
        //     // Designer generated code
        // }
    }
}
